<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            label="数据项名称"
            prop="name"
            :rules="[{ required: true, message: '请输入数据项名称', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="数据项类型"
            prop="sjlx"
            :rules="[{ required: true, message: '请选择数据项类型', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.sjlx" placeholder="请选择数据项类型">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="请选择数据源"
            prop="dataSql.id"
            style="width: 100%"
          >
            <el-select v-model="form.dataSql" placeholder="请选择数据项类型">
              <el-option
                v-for="item in dataSources"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="查询SQL"
            prop="sqlDate"
            :rules="[{ required: true, message: '请输入查询SQL', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input
              v-model="form.sqlDate"
              type="textarea"
              placeholder="请输入查询SQL"
              :rows="2"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="数据测试"
          >
            <el-button
              type="primary"
              :loading="testLoading"
              @click="test('form')"
            >开始测试<i class="el-icon-finished el-icon--right" /></el-button>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="解析项"
            style="width: 100%"
            prop="jxx"
          >
            <el-input v-model="form.jxx" :rows="2" type="textarea" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="查询结果"
            style="width: 100%"
          >
            <el-input
              v-model="queryData"
              readonly
              type="textarea"
              placeholder="请点击‘开始测试’按钮进行sql测试"
              :rows="2"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { saveOrUpdateObject, analysisEntity } from '@/api/mobile/data'
import { queryEnable } from '@/api/system/dataSource'
export default {
  name: 'DataManagerEntity',
  data() {
    return {
      title: '数据项管理',
      loading: false,
      testLoading: false,
      dialogFormVisible: false,
      options: [
        { label: '信息列表查询', value: '1' }
      ],
      dataSources: [],
      form: {
        id: null,
        dataSql: null,
        sqlDate: '',
        isAuth: false,
        name: null,
        jxx: '',
        sjlx: '1'
      },
      queryData: null
    }
  },
  methods: {
    openEntity(data) {
      this.queryData = null
      this.initDataSource()
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = Object.assign({}, data)
        this.title = '修改数据项信息'
        this.dialogFormVisible = true
      } else {
        this.title = '新增数据项信息'
        this.dialogFormVisible = true
      }
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            saveOrUpdateObject(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('更新失败！')
            })
          } else {
            saveOrUpdateObject(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    test(formName) {
      this.$refs[formName].validate((valid) => {
        this.testLoading = true
        if (valid) {
          this.queryData = null
          analysisEntity(this.form).then(res => {
            const { data } = res
            if (undefined !== data && data !== null) {
              this.queryData = JSON.stringify(data.data.data)
              const jxx = JSON.parse(data.entity.jxx)
              this.$nextTick(() => {
                this.form.jxx = JSON.stringify(jxx.data)
              })
            }
            this.testLoading = false
          }).catch(() => {
            this.testLoading = false
            console.log('数据获取失败！')
          })
        } else {
          this.testLoading = false
        }
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    initDataSource() {
      queryEnable().then(res => {
        this.dataSources = res.data.data
      }).catch(() => {
        console.log('初始化数据源信息失败！')
      })
    }
  }
}
</script>

<style scoped>

</style>
